<?php
	class MemberController {
		
		private $m_memberView;
		private $m_memberDAL;
		private $m_memberHandler;
		private $m_boatRegister;
		private $m_searchType;
		
		private $m_searchObject;
		private $m_searchAlternative;
		
		public function __construct ($a_memberView, $a_memberDAL, $a_memberHandler, $a_boatRegister){
			$this->m_memberView = $a_memberView;
			$this->m_memberDAL = $a_memberDAL;
			$this->m_memberHandler = $a_memberHandler;	
			$this->m_boatRegister = $a_boatRegister; 
		}
		
		public function DoControl (){
			$xhtml = "";
			$xhtml .= $this->m_memberView->SearchBox();
			//$this->Search(new SearchAlternativesFactory);

			if($this->m_memberView->TriedToAddMember()){
				$ssn = $this->m_memberView->GetSsn();
				$firstName = $this->m_memberView->GetFirstName();
				$this->m_memberHandler->AddMember($firstName, $ssn);
				}
			if($this->m_memberView->TriedToDeleteMember()){
				
				$ssn = $this->m_memberView->GetSsn();
				
				$memberId = $this->m_memberHandler->GetMemberId($ssn);
				
				$this->m_boatRegister->DeleteBoat($memberId);
				
				$this->m_memberHandler->DeleteMember($memberId);	
			}
			
			$members = $this->m_memberHandler->GetMembers();
			$boatInfoArray = $this->m_boatRegister->GetMemberBoat();
			
			foreach($members as $key => $value){
				$memberId = $value->m_memberId ;
				$value->m_numberOfBoats = $this->m_boatRegister->CountBoats($memberId);
			}
			
			if($this->m_memberView->TriedToEditBoat()){
				$getBoatType = $this->m_memberView->GetBoatType();
				$getBoatInfo = $this->m_memberView->GetBoatInfo();
				$getMemberId = $this->m_memberView->GetMemberId();
				$getBoatLength = $this->m_memberView->GetBoatLength();
				$getBoatId = $this->m_memberView->GetBoatId();
				
				$this->m_boatRegister->UpdateBoat($getBoatInfo, $getBoatType, $getMemberId, $getBoatId, $getBoatLength);
			}
			
			$boatTypesArray = $this->m_boatRegister->GetBoatTypes();
			
			if($this->m_memberView->TriedToSearch()){
				//New instance of searchalternativefactory
				$searchAlternativesFactory = new SearchAlternativesFactory();
				//Get which category is choosen set them in private membervariabels. 
				$this->HandleActiveRadioButton($members, $boatInfoArray, $searchAlternativesFactory);
				$this->m_searchAlternative->Search($this->m_searchObject, $this->m_memberView->GetSearchWord());
			}
			
			if($this->m_memberView->TriedToAddBoatToMember()){
				$boatInfo = $this->m_memberView->GetBoatInfo();
				$memberId = $this->m_memberView->GetMemberId();
				$boatTypeId = $this->m_memberView->GetBoatTypeId();
				$boatLength = $this->m_memberView->GetBoatLength();
				$this->m_boatRegister->AddBoatToMember($boatInfo, $boatTypeId, $memberId, $boatLength);
			}
			
			
			$xhtml .= $this->m_memberView->AddBoatToMember($boatTypesArray);
			
			$xhtml .= $this->m_memberView->RenderCompactMemberList($members);
			
			$xhtml .= $this->m_memberView->RenderMemberList($members, $boatInfoArray);
			
			$xhtml .=  $this->m_memberView->DeleteMemberForm();
			$xhtml .= $this->m_memberView->AddMember();
			$xhtml .= $this->m_memberView->EditBoat($boatTypesArray);
			
				
			return $xhtml;
		}

	private function HandleActiveRadioButton($a_members, $a_boats, $a_searchAlternativesFactory) {
			$navigationView = new NavigationView();	
			$searchAlternativesFactory = $a_searchAlternativesFactory;
			
			
			$activeRadioButton = $navigationView->GetActiveSearchType();	
			
			switch ($activeRadioButton) {
				case MemberView::BOATINFO: 
					$this->m_searchObject = $a_boats;
					$this->m_searchAlternative = $searchAlternativesFactory->IBoatInfoAlternative();
					break;
				
				case MemberView::BOATTYPE:
					$this->m_searchObject = $a_boats;
					$this->m_searchAlternative = $searchAlternativesFactory->IBoatTypeAlternative();
					break;
				case MemberView::BOATLENGTH:
					$this->m_searchObject = $a_boats;
					$this->m_searchAlternative = $searchAlternativesFactory->IBoatLengthAlternative();
					break;
				case MemberView::MEMBERNAME:
					$this->m_searchObject = $a_members;
					$this->m_searchAlternative = $searchAlternativesFactory->IMemberNameAlternative();
					break;
				case MemberView::MEMBERSSN:
					$this->m_searchObject = $a_members;
					$this->m_searchAlternative = $searchAlternativesFactory->IMemberSsnAlternative();
					break;
					
				// No active controller? let's have boatinfo as a default. 
				default:
					$this->m_searchObject = $a_boats;
					$this->m_searchAlternative = $searchAlternativesFactory->IBoatInfoAlternative();
					break;							
			}
			//The active controller generates output, great. 
			return $activeRadioButton;	
		}
	
		public function Search ($a_searchAlternavtivesFactory){
				$m_searchType = $a_searchAlternavtivesFactory->BoatDescriptionAlternative;
			}		
	}